package com.hkc.huakang.hkcpaperless;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Created by My on 2017/4/21.
 */

public class DialogUtils {

    public interface OnCommitListener {
        void retry();
    }
    private DialogUtils(){};

    public   static    DialogUtils  getInstance(){
     return   DialogUtilsHelp.instance;
    }
    private static class DialogUtilsHelp{
        public  static   DialogUtils  instance=new DialogUtils();
    }

    public interface OnProgressView {
        void progressOnProgressView(ProgressBar progressBar);
    }

    public  void showDialog(Context context, String title, String message, final OnCommitListener retry) {
        showDialog(context, title, message, "确定", "取消", retry);
    }

    public  void showDialog(Context context, String title, String message, String submitMessage, String cancleMessage, final OnCommitListener retry) {
        final AlertDialog.Builder bulider = new AlertDialog.Builder(context);
        bulider.setPositiveButton(submitMessage, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                retry.retry();
            }
        });
        bulider.setNegativeButton(cancleMessage, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        bulider.setTitle(title);
        bulider.setMessage(message);
        Dialog dialog = bulider.create();
        dialog.show();
//        setDialogFullScreen(dialog);
    }

    public Dialog createLoadingDialog(Context context, String msg) {
        return createLoadingDialog(context, msg, null);
    }

    public Dialog createLoadingDialog(Context context, String msg, OnProgressView onProgressView) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        TextView tipTextView = (TextView) v.findViewById(R.id.tvMsg);// 提示文字
        ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressbar);
        tipTextView.setText(msg);// 设置加载信息
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);// 不可以用“返回键”取消
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        dialog.show();
        if (progressBar != null && onProgressView != null) {
            onProgressView.progressOnProgressView(progressBar);
        }
//        progressBar.setProgress();
//        setDialogFullScreen(dialog);
        return dialog;
    }

}
